package com.dodock.survey.ui;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TimePicker;

import com.dodock.survey.Brave;
import com.dodock.survey.R;
import com.dodock.survey.adapter.NoDefaultSpinner;
import com.dodock.survey.baseclasses.Information;
import com.dodock.survey.baseclasses.Section1;
import com.dodock.survey.baseclasses.Section2;
import com.dodock.survey.baseclasses.Section3;
import com.dodock.survey.baseclasses.Section4;

public class SectionActivity extends Activity implements
		RadioGroup.OnCheckedChangeListener, OnClickListener {

	LinearLayout sectionContainer;
	private int currently_selected_layout = R.layout.information_layout;
	boolean newForm;
	ScrollView information_layout;
	ScrollView sectionLayout1;
	ScrollView sectionLayout2;
	ScrollView sectionLayout3;
	ScrollView sectionLayout4;

	private int year;
	private int month;
	private int day;

	int currentButtonId = 0;
	int hour;
	int minute;

	final int DATE_DIALOG_ID = 998;
	final int TIME_DIALOG_ID = 999;

	private TextView titleTV;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		newForm = getIntent().getExtras().getBoolean("newForm");
		
		setContentView(R.layout.section);
		
		if(!newForm){
			int itemNo = getIntent().getExtras().getInt("itemNo");
			Brave.USER_NAME = Brave.infoList.get(itemNo).getKey();
			Log.e("geting key db ","item"+itemNo+" , key :"+ Brave.USER_NAME);
		}else{
			Brave.USER_NAME = String.valueOf(System.currentTimeMillis());
			Log.e("setting new key ","key :"+ Brave.USER_NAME);
		}
		titleTV = (TextView) findViewById(R.id.title);
		final Calendar c = Calendar.getInstance();
		previousRadioButtonId = R.id.informationBtn;
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH);
		day = c.get(Calendar.DAY_OF_MONTH);
		hour = c.get(Calendar.HOUR_OF_DAY);
		minute = c.get(Calendar.MINUTE);

		
		Button saveButton = (Button) findViewById(R.id.save);
		saveButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				save();				
			}
		});
		
		Button exitButton = (Button) findViewById(R.id.exit);
		exitButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				onBackPressed();		
			}
		});
		
		RadioGroup sectionRadioGroup = (RadioGroup) findViewById(R.id.sectionRadioGroup);
		sectionRadioGroup.setOnCheckedChangeListener(this);

		RadioButton section3Button = (RadioButton) findViewById(R.id.section3RadioBtn);
		if (Brave.ID_value.equals("B")) {
			section3Button.setEnabled(false);
			section3Button.setFocusable(false);
		} else {
			section3Button.setEnabled(true);
			section3Button.setFocusable(true);
		}
		sectionContainer = (LinearLayout) findViewById(R.id.sectionContainer);
		sectionLayout1 = (ScrollView) LayoutInflater.from(getBaseContext())
				.inflate(R.layout.section1_layout, null);
		sectionLayout2 = (ScrollView) LayoutInflater.from(getBaseContext())
				.inflate(R.layout.section2_layout, null);
		sectionLayout3 = (ScrollView) LayoutInflater.from(getBaseContext())
				.inflate(R.layout.section3_layout, null);
		sectionLayout4 = (ScrollView) LayoutInflater.from(getBaseContext())
				.inflate(R.layout.section4_layout, null);
		information_layout = (ScrollView) LayoutInflater.from(getBaseContext())
				.inflate(R.layout.information_layout, null);
		addViewIntoSectionContainer(R.layout.information_layout);
	}

	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (previousRadioButtonId != checkedId) {
			boolean taskCompleted = storeDataIntoDB(currently_selected_layout,
					STORE);
			if (taskCompleted) {
				previousRadioButtonId = checkedId;
				switch (checkedId) {
				case R.id.informationBtn:
					addViewIntoSectionContainer(R.layout.information_layout);
					break;
				case R.id.section1RadioBtn:
					addViewIntoSectionContainer(R.layout.section1_layout);
					break;
				case R.id.section2RadioBtn:
					addViewIntoSectionContainer(R.layout.section2_layout);
					break;
				case R.id.section3RadioBtn:
					addViewIntoSectionContainer(R.layout.section3_layout);
					break;
				case R.id.section4RadioBtn:
					addViewIntoSectionContainer(R.layout.section4_layout);
					break;
				default:
					break;
				}
			} else {
				if (previousRadioButtonId != 0) {
					group.check(previousRadioButtonId);
				}
			}
		}
	}

	int previousRadioButtonId = 0;

	private final int SHOW = 77;
	private final int STORE = 78;

	private boolean storeDataIntoDB(int layoutId, int task) {
		boolean taskCompleted = false;
		switch (layoutId) {
		case R.layout.information_layout:
			taskCompleted = storeFetchInformationData(task);
			break;
		case R.layout.section1_layout:
			taskCompleted = storeFetchSection1Data(task);
			break;
		case R.layout.section2_layout:
			taskCompleted = storeFetchSection2Data(task);
			break;
		case R.layout.section3_layout:
			taskCompleted = storeFetchSection3Data(task);
			break;
		case R.layout.section4_layout:
			taskCompleted = storeFetchSection4Data(task);
			break;
		default:
			break;
		}
		return taskCompleted;
	}
	private boolean storeFetchInformationData(int task) {
		boolean taskCompleted = false;
		// TODO Auto-generated method stub
		EditText infoAorB = (EditText) findViewById(R.id.infoAorB);
		infoAorB.setOnFocusChangeListener(infoETFocusChangeListner);
		EditText facilityCode = (EditText) findViewById(R.id.facilityCode);
		facilityCode.setOnFocusChangeListener(infoETFocusChangeListner);
		EditText subjectId = (EditText) findViewById(R.id.subjectId);
		subjectId.setOnFocusChangeListener(infoETFocusChangeListner);
		EditText subjectName = (EditText) findViewById(R.id.subjectName);
		subjectName.setOnFocusChangeListener(infoETFocusChangeListner);
		EditText infoAddOfSubj1 = (EditText) findViewById(R.id.addressOfSubj);
		infoAddOfSubj1.setOnFocusChangeListener(infoETFocusChangeListner);
		EditText infoNameOfHospital = (EditText) findViewById(R.id.nameOfHospital);
		infoNameOfHospital.setOnFocusChangeListener(infoETFocusChangeListner);		
		EditText infoWordNoOrOPD = (EditText) findViewById(R.id.wordNoOpd);
		infoWordNoOrOPD.setOnFocusChangeListener(infoETFocusChangeListner);
		EditText infoHospitalRegNo = (EditText) findViewById(R.id.hospitalRegNo);
		infoHospitalRegNo.setOnFocusChangeListener(infoETFocusChangeListner);
		EditText info1stInterviewerNameOrCode = (EditText) findViewById(R.id.firstInerviewNameCode);
		info1stInterviewerNameOrCode.setOnFocusChangeListener(infoETFocusChangeListner);
		EditText info2ndInterviewerNameOrCode = (EditText) findViewById(R.id.secondInterviewerNameCode);
		CheckBox infoUsr1sectionFilled1 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled1);
		CheckBox infoUsr1sectionFilled2 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled2);
		CheckBox infoUsr1sectionFilled3 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled3);
		CheckBox infoUsr1sectionFilled4 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled4);
		CheckBox infoUsr1sectionFilled5 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled5);
		CheckBox infoUsr1sectionFilled6 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled6);
		CheckBox infoUsr1sectionFilled7 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled7);
		CheckBox infoUsr1sectionFilled8 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled8);		
		CheckBox infoUsr1sectionFilled9 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled9);
		CheckBox infoUsr1sectionFilled10 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled10);
		CheckBox infoUsr1sectionFilled11 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled11);
		CheckBox infoUsr1sectionFilled12 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled12);
		CheckBox infoUsr1sectionFilled13 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled13);
		CheckBox infoUsr1sectionFilled14 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled14);
		CheckBox infoUsr1sectionFilled15 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled15);
		CheckBox infoUsr2sectionFilled1 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled1);
		CheckBox infoUsr2sectionFilled2 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled2);
		CheckBox infoUsr2sectionFilled3 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled3);
		CheckBox infoUsr2sectionFilled4 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled4);
		CheckBox infoUsr2sectionFilled5 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled5);
		CheckBox infoUsr2sectionFilled6 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled6);
		CheckBox infoUsr2sectionFilled7 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled7);
		CheckBox infoUsr2sectionFilled8 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled8);		
		CheckBox infoUsr2sectionFilled9 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled9);
		CheckBox infoUsr2sectionFilled10 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled10);
		CheckBox infoUsr2sectionFilled11 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled11);
		CheckBox infoUsr2sectionFilled12 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled12);
		CheckBox infoUsr2sectionFilled13 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled13);
		CheckBox infoUsr2sectionFilled14 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled14);
		CheckBox infoUsr2sectionFilled15 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled15);

		RadioGroup infoDataColRG = (RadioGroup) findViewById(R.id.infoDataColRG);
		infoDataColRG.setOnCheckedChangeListener(infoRGChangeListner);
		RadioButton infoDataColCompleteRB = (RadioButton) findViewById(R.id.infoDataColCompleteRB);
		RadioButton infoDataColIncompleteRB = (RadioButton) findViewById(R.id.infoDataColIncompleteRB);
		
		NoDefaultSpinner infoReasonForIncomplete = (NoDefaultSpinner) findViewById(R.id.infoReasonForIncomplete);
		infoReasonForIncomplete.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				// TODO Auto-generated method stub
				
				arg0.requestFocus();
				return false;
			}
		});
		Button infoDateOfEnrolement = (Button) findViewById(R.id.dateOfEnrolmentButton);
		infoDateOfEnrolement.setOnClickListener(this);

		Button infoDateOfInterview = (Button) findViewById(R.id.dateOfInterview);
		infoDateOfInterview.setOnClickListener(this);

		Button infoDateOfSampling = (Button) findViewById(R.id.dateOfSampling);
		infoDateOfSampling.setOnClickListener(this);

		Button infoDateOfLastMeal = (Button) findViewById(R.id.dateOfLastMeal);
		infoDateOfLastMeal.setOnClickListener(this);

		Button infoEndDate = (Button) findViewById(R.id.endDate);
		infoEndDate.setOnClickListener(this);

		Button infoEndTime = (Button) findViewById(R.id.endTime);
		infoEndTime.setOnClickListener(this);

		Button infoStartTime = (Button) findViewById(R.id.startTime);
		infoStartTime.setOnClickListener(this);

		Button infoTimeOfSampling = (Button) findViewById(R.id.timeOfSampling);
		infoTimeOfSampling.setOnClickListener(this);
		
		Button infoTimeOfLastMeal= (Button) findViewById(R.id.timeOfLastMeal);
		infoTimeOfLastMeal.setOnClickListener(this);
		
		if (task == STORE) {
			if(infoAorB.getText().toString().equals("A") || infoAorB.getText().toString().equals("B")||
					infoAorB.getText().toString().equals("")){
				Brave.ID_value = infoAorB.getText().toString();
			}else{
				openErrorDialog("Wrong id");
				infoAorB.getText().clear();
				infoAorB.requestFocus();
				taskCompleted = false;
				return false;
			}
			ContentValues cv = new ContentValues();
			cv.put("key", Brave.encryptString(Brave.USER_NAME));
			cv.put("infoId", Brave.encryptString(infoAorB.getText().toString()));
			cv.put("infoFacilityCode", Brave.encryptString(facilityCode.getText().toString()));
			cv.put("infoSubjectId", Brave.encryptString(subjectId.getText().toString()));
			cv.put("infoSubjectName", Brave.encryptString(subjectName.getText().toString()));
			Log.e("putting name", ""+subjectName.getText().toString());
			cv.put("infoAddOfSubj1", Brave.encryptString(infoAddOfSubj1.getText().toString()));
			cv.put("infoNameOfHospital", Brave.encryptString(infoNameOfHospital.getText().toString()));
			cv.put("infoWordNoOrOPD", Brave.encryptString(infoWordNoOrOPD.getText().toString()));
			cv.put("infoHospitalRegNo", Brave.encryptString(infoHospitalRegNo.getText().toString()));
			cv.put("info1stInterviewerNameOrCode", Brave.encryptString(info1stInterviewerNameOrCode.getText().toString()));
			cv.put("info2ndInterviewerNameOrCode", Brave.encryptString(info2ndInterviewerNameOrCode.getText().toString()));
			cv.put("infoUsr1sectionFilled1", Brave.encryptInteger(infoUsr1sectionFilled1.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled2", Brave.encryptInteger(infoUsr1sectionFilled2.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled3", Brave.encryptInteger(infoUsr1sectionFilled3.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled4", Brave.encryptInteger(infoUsr1sectionFilled4.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled5", Brave.encryptInteger(infoUsr1sectionFilled5.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled6", Brave.encryptInteger(infoUsr1sectionFilled6.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled7", Brave.encryptInteger(infoUsr1sectionFilled7.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled8", Brave.encryptInteger(infoUsr1sectionFilled8.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled9", Brave.encryptInteger(infoUsr1sectionFilled9.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled10", Brave.encryptInteger(infoUsr1sectionFilled10.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled11", Brave.encryptInteger(infoUsr1sectionFilled11.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled12", Brave.encryptInteger(infoUsr1sectionFilled12.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled13", Brave.encryptInteger( infoUsr1sectionFilled13.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled14", Brave.encryptInteger(infoUsr1sectionFilled14.isChecked() == true ? 1 : 0));
			cv.put("infoUsr1sectionFilled15", Brave.encryptInteger(infoUsr1sectionFilled15.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled1", Brave.encryptInteger(infoUsr2sectionFilled1.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled2", Brave.encryptInteger( infoUsr2sectionFilled2.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled3", Brave.encryptInteger(infoUsr2sectionFilled3.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled4", Brave.encryptInteger(infoUsr2sectionFilled4.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled5", Brave.encryptInteger(infoUsr2sectionFilled5.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled6", Brave.encryptInteger(infoUsr2sectionFilled6.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled7", Brave.encryptInteger(infoUsr2sectionFilled7.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled8", Brave.encryptInteger(infoUsr2sectionFilled8.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled9", Brave.encryptInteger(infoUsr2sectionFilled9.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled10", Brave.encryptInteger(infoUsr2sectionFilled10.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled11", Brave.encryptInteger(infoUsr2sectionFilled11.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled12", Brave.encryptInteger(infoUsr2sectionFilled12.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled13", Brave.encryptInteger(infoUsr2sectionFilled13.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled14", Brave.encryptInteger(infoUsr2sectionFilled14.isChecked() == true ? 1 : 0));
			cv.put("infoUsr2sectionFilled15", Brave.encryptInteger(infoUsr2sectionFilled15.isChecked() == true ? 1 : 0));
			int dataColStatus = 0;
			if(infoDataColCompleteRB.isChecked())
				dataColStatus = 1;
			else if(infoDataColIncompleteRB.isSelected())
				dataColStatus = 2;
			cv.put("infoDataCollectionStatus", Brave.encryptInteger(dataColStatus));
			cv.put("infoReasonForIncomplete", Brave.encryptInteger(infoReasonForIncomplete.getSelectedItemPosition()));
			cv.put("infoDateOfEnrolement", Brave.encryptString(infoDateOfEnrolement.getText().toString()));
			cv.put("infoDateOfInterview", Brave.encryptString(infoDateOfInterview.getText().toString()));
			cv.put("infoDateOfSampling", Brave.encryptString(infoDateOfSampling.getText().toString()));
			cv.put("infoDateOfLastMeal", Brave.encryptString(infoDateOfLastMeal.getText().toString()));
			cv.put("infoEndDate", Brave.encryptString(infoEndDate.getText().toString()));
			cv.put("infoEndTime", Brave.encryptString(infoEndTime.getText().toString()));
			cv.put("infoStartTime", Brave.encryptString(infoStartTime.getText().toString()));		
			cv.put("infoTimeOfSampling", Brave.encryptString(infoTimeOfSampling.getText().toString()));		
			cv.put("infoTimeOfLastMeal", Brave.encryptString(infoTimeOfLastMeal.getText().toString()));
			Brave.getDatabaseManager().addInformatonIntoDB(cv , Brave.USER_NAME);
			taskCompleted = true;
		} else if (task == SHOW) {
			titleTV.setText(R.string.information);
			Log.e("Brave.USER_NAME", ""+Brave.USER_NAME);
			Information information = Brave.getDatabaseManager().getInformation(Brave.USER_NAME);
			if(information!=null){
				Log.e("SectionActivity", "information not null from database");
				infoAorB.setText(information.getInfoId());
				facilityCode.setText(information.getInfoFacilityCode());
				subjectId.setText(information.getInfoSubjectId());
				subjectName.setText(information.getInfoSubjectName());
				Log.e("getName", ""+information.getInfoSubjectName());
				infoAddOfSubj1.setText(information.getInfoAddressAndContactNumbOfSubj());
				infoNameOfHospital.setText(information.getInfoNameOfHospital());
				infoWordNoOrOPD.setText(information.getInfoWordNoOrOPD());
				infoHospitalRegNo.setText(information.getInfoHospitalRegNo());
				info1stInterviewerNameOrCode.setText(information.getInfoInterviewerNameOrCode());
				info2ndInterviewerNameOrCode.setText(information.getInfoSecondInterviewerNameOrCode());
				infoUsr1sectionFilled1.setChecked(information.getInfoUsr1sectionFilled1() == 1 ? true : false);
				infoUsr1sectionFilled2.setChecked(information.getInfoUsr1sectionFilled2() == 1 ? true : false);
				infoUsr1sectionFilled3.setChecked(information.getInfoUsr1sectionFilled3() == 1 ? true : false);
				infoUsr1sectionFilled4.setChecked(information.getInfoUsr1sectionFilled4() == 1 ? true : false);
				infoUsr1sectionFilled5.setChecked(information.getInfoUsr1sectionFilled5() == 1 ? true : false);
				infoUsr1sectionFilled6.setChecked(information.getInfoUsr1sectionFilled6() == 1 ? true : false);
				infoUsr1sectionFilled7.setChecked(information.getInfoUsr1sectionFilled7() == 1 ? true : false);
				infoUsr1sectionFilled8.setChecked(information.getInfoUsr1sectionFilled8() == 1 ? true : false);
				infoUsr1sectionFilled9.setChecked(information.getInfoUsr1sectionFilled9() == 1 ? true : false);
				infoUsr1sectionFilled10.setChecked(information.getInfoUsr1sectionFilled10() == 1 ? true : false);
				infoUsr1sectionFilled11.setChecked(information.getInfoUsr1sectionFilled11() == 1 ? true : false);
				infoUsr1sectionFilled12.setChecked(information.getInfoUsr1sectionFilled12() == 1 ? true : false);
				infoUsr1sectionFilled13.setChecked(information.getInfoUsr1sectionFilled13() == 1 ? true : false);
				infoUsr1sectionFilled14.setChecked(information.getInfoUsr1sectionFilled14() == 1 ? true : false);
				infoUsr1sectionFilled15.setChecked(information.getInfoUsr1sectionFilled15() == 1 ? true : false);
				infoUsr2sectionFilled1.setChecked(information.getInfoUsr2sectionFilled1() == 1 ? true : false);
				infoUsr2sectionFilled2.setChecked(information.getInfoUsr2sectionFilled2() == 1 ? true : false);
				infoUsr2sectionFilled3.setChecked(information.getInfoUsr2sectionFilled3() == 1 ? true : false);
				infoUsr2sectionFilled4.setChecked(information.getInfoUsr2sectionFilled4() == 1 ? true : false);
				infoUsr2sectionFilled5.setChecked(information.getInfoUsr2sectionFilled5() == 1 ? true : false);
				infoUsr2sectionFilled6.setChecked(information.getInfoUsr2sectionFilled6() == 1 ? true : false);
				infoUsr2sectionFilled7.setChecked(information.getInfoUsr2sectionFilled7() == 1 ? true : false);
				infoUsr2sectionFilled8.setChecked(information.getInfoUsr2sectionFilled8() == 1 ? true : false);
				infoUsr2sectionFilled9.setChecked(information.getInfoUsr2sectionFilled9() == 1 ? true : false);
				infoUsr2sectionFilled10.setChecked(information.getInfoUsr2sectionFilled10() == 1 ? true : false);
				infoUsr2sectionFilled11.setChecked(information.getInfoUsr2sectionFilled11() == 1 ? true : false);
				infoUsr2sectionFilled12.setChecked(information.getInfoUsr2sectionFilled12() == 1 ? true : false);
				infoUsr2sectionFilled13.setChecked(information.getInfoUsr2sectionFilled13() == 1 ? true : false);
				infoUsr2sectionFilled14.setChecked(information.getInfoUsr2sectionFilled14() == 1 ? true : false);
				infoUsr2sectionFilled15.setChecked(information.getInfoUsr2sectionFilled15() == 1 ? true : false);
				
				if(information.getInfoDataCollectionStatus() == 1){
					infoDataColCompleteRB.setChecked(true);
					infoReasonForIncomplete.setSelection(0);
					infoReasonForIncomplete.setEnabled(false);					
				}else if(information.getInfoDataCollectionStatus() == 2){
					infoDataColIncompleteRB.setChecked(true);
					infoReasonForIncomplete.setEnabled(true);
					infoReasonForIncomplete.setSelection(information.getInfoReasonForIncomplete());
				}
				
				
				
				if(information.getInfoDateOfEnrolement() != null && information.getInfoDateOfEnrolement().length() > 0)
					infoDateOfEnrolement.setText(information.getInfoDateOfEnrolement());	
				else
					infoDateOfEnrolement.setText(day+"-"+(month + 1)+"-"+year);

				if(information.getInfoDateOfInterview() != null && information.getInfoDateOfInterview().length() > 0)
					infoDateOfInterview.setText(information.getInfoDateOfInterview());
				else
					infoDateOfInterview.setText(day+"-"+(month + 1)+"-"+year);
					
				if(information.getInfoDateOfSampling() != null && information.getInfoDateOfSampling().length() > 0) 
					infoDateOfSampling.setText(information.getInfoDateOfSampling());
				else
					infoDateOfSampling.setText(day+"-"+(month + 1)+"-"+year);
				
				if(information.getInfoDateOfLastMeal() != null && information.getInfoDateOfLastMeal().length() > 0)
					infoDateOfLastMeal.setText(information.getInfoDateOfLastMeal());
				else
					infoDateOfLastMeal.setText(day+"-"+(month + 1)+"-"+year);
				
				if(information.getInfoEndDate()!= null && information.getInfoEndDate().length() > 0)
					infoEndDate.setText(information.getInfoEndDate());
				else
					infoEndDate.setText( day + "-" + (month + 1) + "-" + year );
				
				if(information.getInfoEndTime()!=null && information.getInfoEndTime().length() > 0)
					infoEndTime.setText(information.getInfoEndTime());
				else
					infoEndTime.setText( hour + ":" + minute );
				
				if(information.getInfoStartTime() !=null && information.getInfoStartTime().length() >0)
					infoStartTime.setText(information.getInfoStartTime());
				else
					infoStartTime.setText( hour + ":" +minute );
				if(information.getInfoTimeOfSampling() != null && information.getInfoTimeOfSampling().length() > 0)
					infoTimeOfSampling.setText(information.getInfoTimeOfSampling());
				else
					infoTimeOfSampling.setText( hour + ":" + minute );
				if(information.getInfoTimeOfLastMeal()!= null && information.getInfoTimeOfLastMeal().length() > 0)
					infoTimeOfLastMeal.setText(information.getInfoTimeOfLastMeal());
				else
					infoTimeOfLastMeal.setText( hour + ":" + minute );
			}else{
				Log.e("SectionActivity", "information null from database");
				infoDateOfEnrolement.setText(day+"-"+(month + 1)+"-"+year);
				infoDateOfInterview.setText(day+"-"+(month + 1)+"-"+year);
				infoDateOfSampling.setText(day+"-"+(month + 1)+"-"+year);
				infoDateOfLastMeal.setText(day+"-"+(month + 1)+"-"+year);
				infoEndDate.setText( day + "-" + (month + 1) + "-" + year );
				infoEndTime.setText( hour + ":" + minute );
				infoStartTime.setText( hour + ":" +minute );
				infoTimeOfSampling.setText( hour + ":" + minute );
				infoTimeOfLastMeal.setText( hour + ":" + minute );
				
				infoReasonForIncomplete.setEnabled(false);
			}
			taskCompleted = true;
		}
		return taskCompleted;
	}

	
	
	private boolean storeFetchSection1Data(int task) {
		boolean taskCompleted = false;
		// TODO Auto-generated method stub
		EditText systolicBloodPressure1 = (EditText) sectionLayout1
				.findViewById(R.id.systolicBloodPressure1);
		systolicBloodPressure1
				.setOnFocusChangeListener(section1ETFocusChangeListner);
		EditText systolicBloodPressure2 = (EditText) sectionLayout1
				.findViewById(R.id.systolicBloodPressure2);
		systolicBloodPressure2
				.setOnFocusChangeListener(section1ETFocusChangeListner);
		EditText diastolicBloodPressure1 = (EditText) sectionLayout1
				.findViewById(R.id.diastolicBloodPressure1);
		diastolicBloodPressure1
				.setOnFocusChangeListener(section1ETFocusChangeListner);
		EditText diastolicBloodPressure2 = (EditText) sectionLayout1
				.findViewById(R.id.diastolicBloodPressure2);
		diastolicBloodPressure2
				.setOnFocusChangeListener(section1ETFocusChangeListner);
		EditText wrist = (EditText) sectionLayout1.findViewById(R.id.wrist);
		wrist.setOnFocusChangeListener(section1ETFocusChangeListner);
		EditText hip = (EditText) sectionLayout1.findViewById(R.id.hip);
		hip.setOnFocusChangeListener(section1ETFocusChangeListner);
		EditText weight = (EditText) sectionLayout1.findViewById(R.id.weight);
		weight.setOnFocusChangeListener(section1ETFocusChangeListner);
		EditText height = (EditText) sectionLayout1.findViewById(R.id.height);
		height.setOnFocusChangeListener(section1ETFocusChangeListner);
		EditText heart_rate = (EditText) sectionLayout1
				.findViewById(R.id.heart_rate);
		heart_rate.setOnFocusChangeListener(section1ETFocusChangeListner);
		if (task == STORE) {
			ContentValues cv = new ContentValues();
			cv.put("key", Brave.encryptString(Brave.USER_NAME));
			if (systolicBloodPressure1.getText().length() > 0) {
				if (Long.valueOf(systolicBloodPressure1.getText().toString()) < 70
						|| Long.valueOf(systolicBloodPressure1.getText()
								.toString()) > 260) {
					systolicBloodPressure1.getText().clear();
					systolicBloodPressure1.requestFocus();					
					openErrorDialog("Systolic blood pressure");
					return false;
				}
			}

			cv.put("systolicBloodPressure1", Brave
					.encryptString(systolicBloodPressure1.getText().toString()));
			if (systolicBloodPressure2.getText().length() > 0) {
				if (Long.valueOf(systolicBloodPressure2.getText().toString()) < 70
						|| Long.valueOf(systolicBloodPressure2.getText()
								.toString()) > 260) {
					systolicBloodPressure2.getText().clear();
					systolicBloodPressure2.requestFocus();
					openErrorDialog("Systolic blood pressure 2");
					return false;
				}
			}
			cv.put("systolicBloodPressure2", Brave
					.encryptString(systolicBloodPressure2.getText().toString()));
			if (diastolicBloodPressure1.getText().length() > 0) {
				if (Long.valueOf(diastolicBloodPressure1.getText().toString()) < 30
						|| Long.valueOf(diastolicBloodPressure1.getText()
								.toString()) > 150) {
					diastolicBloodPressure1.getText().clear();
					diastolicBloodPressure1.requestFocus();
					openErrorDialog("Diastolic blood pressure");
					return false;
				}
			}
			cv.put("diastolicBloodPressure1",
					Brave.encryptString(diastolicBloodPressure1.getText()
							.toString()));
			if (diastolicBloodPressure2.getText().length() > 0) {
				if (Long.valueOf(diastolicBloodPressure2.getText().toString()) < 30
						|| Long.valueOf(diastolicBloodPressure2.getText()
								.toString()) > 150) {
					diastolicBloodPressure2.getText().clear();
					diastolicBloodPressure2.requestFocus();
					openErrorDialog("Diastolic blood pressure 2");
					return false;
				}
			}
			cv.put("diastolicBloodPressure2",
					Brave.encryptString(diastolicBloodPressure2.getText()
							.toString()));
			if (wrist.getText().length() > 0) {
				if (Long.valueOf(wrist.getText().toString()) < 70
						|| Long.valueOf(wrist.getText().toString()) > 130) {
					wrist.getText().clear();
					wrist.requestFocus();
					openErrorDialog("Wrist");
					return false;
				}
			}
			cv.put("wrist", Brave.encryptString(wrist.getText().toString()));
			if (hip.getText().length() > 0) {
				if (Long.valueOf(hip.getText().toString()) < 70
						|| Long.valueOf(hip.getText().toString()) > 130) {
					hip.getText().clear();
					hip.requestFocus();
					openErrorDialog("Hip");
					return false;
				}
			}
			cv.put("hip", Brave.encryptString(hip.getText().toString()));
			if (weight.getText().length() > 0) {
				if (Long.valueOf(weight.getText().toString()) < 40
						|| Long.valueOf(weight.getText().toString()) > 150) {
					weight.getText().clear();
					weight.requestFocus();
					openErrorDialog("Weight");
					return false;
				}
			}
			cv.put("weight", Brave.encryptString(weight.getText().toString()));
			if (height.getText().length() > 0) {
				if (Long.valueOf(height.getText().toString()) < 130
						|| Long.valueOf(height.getText().toString()) > 220) {
					height.getText().clear();
					height.requestFocus();
					openErrorDialog("Height");
					return false;
				}
			}
			cv.put("height", Brave.encryptString(height.getText().toString()));
			if (heart_rate.getText().length() > 0) {
				if (Long.valueOf(heart_rate.getText().toString()) < 60
						|| Long.valueOf(heart_rate.getText().toString()) > 90) {
					heart_rate.getText().clear();
					heart_rate.requestFocus();
					openErrorDialog("Heart rate");
					return false;
				}
			}
			cv.put("heart_rate",
					Brave.encryptString(heart_rate.getText().toString()));

			Brave.getDatabaseManager().addSection1DataIntoDB(cv,
					Brave.USER_NAME);
			taskCompleted = true;
		} else if (task == SHOW) {
			titleTV.setText(R.string.physical_measurments);
			Section1 section1 = Brave.getDatabaseManager().getSection1Data(
					Brave.USER_NAME);
			if (section1 != null) {
				Log.e("", "section1 not null");
				if (section1.getSystolicBloodPressure1() > 0)
					systolicBloodPressure1.setText(""
							+ section1.getSystolicBloodPressure1());
				if (section1.getSystolicBloodPressure2() > 0)
					systolicBloodPressure2.setText(""
							+ section1.getSystolicBloodPressure2());
				if (section1.getDiastolicBloodPressure1() > 0)
					diastolicBloodPressure1.setText(""
							+ section1.getDiastolicBloodPressure1());
				if (section1.getDiastolicBloodPressure2() > 0)
					diastolicBloodPressure2.setText(""
							+ section1.getDiastolicBloodPressure2());
				if (section1.getWrist() > 0)
					wrist.setText("" + section1.getWrist());
				if (section1.getHip() > 0)
					hip.setText("" + section1.getHip());
				if (section1.getWeight() > 0)
					weight.setText("" + section1.getWeight());
				if (section1.getHeight() > 0)
					height.setText("" + section1.getHeight());
				if (section1.getHeart_rate() > 0)
					heart_rate.setText("" + section1.getHeart_rate());
			}
			taskCompleted = true;
		}
		return taskCompleted;
	}

	int currentHour = 0;
	int currentMin = 0;
	int currentMonth = 0;
	int currentDay = 0;
	int currentYear = 0;

	void getCurrentTime(String time) {
		final String SPLIT_STR = ":";
		final String[] splitStr = time.split(SPLIT_STR);
		currentHour = Integer.parseInt(splitStr[0]);
		currentMin = Integer.parseInt(splitStr[1]);
	}

	void getCurrentDate(String date) {
		final String SPLIT_STR = "-";
		final String[] splitStr = date.split(SPLIT_STR);
		currentDay = Integer.parseInt(splitStr[0]);
		currentMonth = Integer.parseInt(splitStr[1]) - 1;
		currentYear = Integer.parseInt(splitStr[2]);
	}

	private RadioGroup.OnCheckedChangeListener section4RGChangeListner = new RadioGroup.OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			switch (checkedId) {
			case R.id.ph1RB1:
				EditText ph1year = (EditText) sectionLayout4
						.findViewById(R.id.ph1yearET);
				ph1year.setEnabled(true);
				ph1year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				break;
			case R.id.ph1RB2:
				ph1year = (EditText) sectionLayout4
						.findViewById(R.id.ph1yearET);
				ph1year.getText().clear();
				ph1year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph1year.setEnabled(false);
				break;
			case R.id.ph2RB1:
				EditText ph2year = (EditText) sectionLayout4
						.findViewById(R.id.ph2yearET);
				ph2year.setEnabled(true);
				ph2year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				break;
			case R.id.ph2RB2:
				ph2year = (EditText) sectionLayout4
						.findViewById(R.id.ph2yearET);
				ph2year.getText().clear();
				ph2year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph2year.setEnabled(false);
				break;
			case R.id.ph3RB1:
				EditText ph3year = (EditText) sectionLayout4
						.findViewById(R.id.ph3yearET);
				ph3year.setEnabled(true);
				ph3year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				break;
			case R.id.ph3RB2:
				ph3year = (EditText) sectionLayout4
						.findViewById(R.id.ph3yearET);
				ph3year.getText().clear();
				ph3year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph3year.setEnabled(false);
				break;
			case R.id.ph4RB1:
				EditText ph4year = (EditText) sectionLayout4
						.findViewById(R.id.ph4yearET);
				ph4year.setEnabled(true);
				ph4year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				break;
			case R.id.ph4RB2:
				ph4year = (EditText) sectionLayout4
						.findViewById(R.id.ph4yearET);
				ph4year.getText().clear();
				ph4year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph4year.setEnabled(false);
				break;
			case R.id.ph5RB1:
				EditText ph5year = (EditText) sectionLayout4
						.findViewById(R.id.ph5year);
				ph5year.setEnabled(true);
				ph5year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				break;
			case R.id.ph5RB2:
				ph5year = (EditText) sectionLayout4.findViewById(R.id.ph5year);
				ph5year.getText().clear();
				ph5year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph5year.setEnabled(false);
				break;
			case R.id.ph6RB1:
				EditText ph6year = (EditText) sectionLayout4
						.findViewById(R.id.ph6year);
				ph6year.setEnabled(true);
				ph6year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				break;
			case R.id.ph6RB2:
				ph6year = (EditText) sectionLayout4.findViewById(R.id.ph6year);
				ph6year.getText().clear();
				ph6year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph6year.setEnabled(false);
				break;
			case R.id.ph7RB1:
				EditText ph7year = (EditText) sectionLayout4
						.findViewById(R.id.ph7year);
				ph7year.setEnabled(true);
				ph7year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				break;
			case R.id.ph7RB2:
				ph7year = (EditText) sectionLayout4.findViewById(R.id.ph7year);
				ph7year.getText().clear();
				ph7year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph7year.setEnabled(false);
				break;
			case R.id.ph8RB1:
				EditText ph8year = (EditText) sectionLayout4
						.findViewById(R.id.ph8year);
				ph8year.setEnabled(true);
				ph8year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				break;
			case R.id.ph8RB2:
				ph8year = (EditText) sectionLayout4.findViewById(R.id.ph8year);
				ph8year.getText().clear();
				ph8year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph8year.setEnabled(false);
				break;
			case R.id.ph9RB1:
				EditText ph9year = (EditText) sectionLayout4
						.findViewById(R.id.ph9year);
				ph9year.setEnabled(true);
				ph9year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				break;
			case R.id.ph9RB2:
				ph9year = (EditText) sectionLayout4.findViewById(R.id.ph9year);
				ph9year.getText().clear();
				ph9year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph9year.setEnabled(false);
				break;
			case R.id.ph10RB1:
				EditText ph10year = (EditText) sectionLayout4
						.findViewById(R.id.ph10year);
				ph10year.setEnabled(true);
				ph10year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				break;
			case R.id.ph10RB2:
				ph10year = (EditText) sectionLayout4
						.findViewById(R.id.ph10year);
				ph10year.getText().clear();
				ph10year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph10year.setEnabled(false);
				break;
			case R.id.ph11RB1:
				EditText ph11year = (EditText) sectionLayout4
						.findViewById(R.id.ph11year);
				ph11year.setEnabled(true);
				ph11year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				break;
			case R.id.ph11RB2:
				ph11year = (EditText) sectionLayout4
						.findViewById(R.id.ph11year);
				ph11year.getText().clear();
				ph11year.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph11year.setEnabled(false);
				break;
			case R.id.ph12RB1:
				RadioButton rb = (RadioButton) sectionLayout4
						.findViewById(R.id.ph13RB1);
				rb.setEnabled(true);
				rb = (RadioButton) sectionLayout4.findViewById(R.id.ph13RB2);
				rb.setEnabled(true);
				rb = (RadioButton) sectionLayout4.findViewById(R.id.ph13RB3);
				rb.setEnabled(true);
				break;
			case R.id.ph12RB2:
				// same as ph12rb3
			case R.id.ph12RB3:
				RadioGroup rg = (RadioGroup) sectionLayout4
						.findViewById(R.id.ph13RG);
				rg.clearCheck();

				rb = (RadioButton) sectionLayout4.findViewById(R.id.ph13RB1);
				rb.setEnabled(false);
				rb = (RadioButton) sectionLayout4.findViewById(R.id.ph13RB2);
				rb.setEnabled(false);
				rb = (RadioButton) sectionLayout4.findViewById(R.id.ph13RB3);
				rb.setEnabled(false);

				EditText ph14value = (EditText) sectionLayout4
						.findViewById(R.id.ph14value);
				ph14value.getText().clear();
				ph14value.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph14value.setEnabled(false);
				break;

			case R.id.ph13RB1:
				ph14value = (EditText) sectionLayout4
						.findViewById(R.id.ph14value);
				ph14value.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				ph14value.setEnabled(true);
				break;
			case R.id.ph13RB2:
				// same as ph12rb3
			case R.id.ph13RB3:
				ph14value = (EditText) sectionLayout4
						.findViewById(R.id.ph14value);
				ph14value.getText().clear();
				ph14value.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				ph14value.setEnabled(false);
				break;
			default:
				break;
			}
		}
	};

	private boolean storeFetchSection4Data(int task) {
		// TODO Auto-generated method stub
		boolean taskCompleted = false;
		RadioGroup ph1RG = (RadioGroup) sectionLayout4.findViewById(R.id.ph1RG);
		ph1RG.setOnCheckedChangeListener(section4RGChangeListner);

		RadioButton ph1RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph1RB1);
		RadioButton ph1RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph1RB2);
		EditText ph1year = (EditText) sectionLayout4
				.findViewById(R.id.ph1yearET);
		ph1year.setOnFocusChangeListener(section4ETFocusChangeListner);

		CheckBox ph1CB1 = (CheckBox) sectionLayout4.findViewById(R.id.ph1CB1);
		CheckBox ph1CB2 = (CheckBox) sectionLayout4.findViewById(R.id.ph1CB2);
		CheckBox ph1CB3 = (CheckBox) sectionLayout4.findViewById(R.id.ph1CB3);
		CheckBox ph1CB4 = (CheckBox) sectionLayout4.findViewById(R.id.ph1CB4);
		CheckBox ph1CB5 = (CheckBox) sectionLayout4.findViewById(R.id.ph1CB5);

		RadioGroup ph2RG = (RadioGroup) sectionLayout4.findViewById(R.id.ph2RG);
		ph2RG.setOnCheckedChangeListener(section4RGChangeListner);
		RadioButton ph2RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph2RB1);
		RadioButton ph2RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph2RB2);
		EditText ph2year = (EditText) sectionLayout4
				.findViewById(R.id.ph2yearET);
		ph2year.setOnFocusChangeListener(section4ETFocusChangeListner);

		CheckBox ph2CB1 = (CheckBox) sectionLayout4.findViewById(R.id.ph2CB1);
		CheckBox ph2CB2 = (CheckBox) sectionLayout4.findViewById(R.id.ph2CB2);
		CheckBox ph2CB3 = (CheckBox) sectionLayout4.findViewById(R.id.ph2CB3);

		RadioGroup ph3RG = (RadioGroup) sectionLayout4.findViewById(R.id.ph3RG);
		ph3RG.setOnCheckedChangeListener(section4RGChangeListner);
		RadioButton ph3RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph3RB1);
		RadioButton ph3RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph3RB2);
		EditText ph3year = (EditText) sectionLayout4
				.findViewById(R.id.ph3yearET);
		ph3year.setOnFocusChangeListener(section4ETFocusChangeListner);

		RadioGroup ph4RG = (RadioGroup) sectionLayout4.findViewById(R.id.ph4RG);
		ph4RG.setOnCheckedChangeListener(section4RGChangeListner);
		RadioButton ph4RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph4RB1);
		RadioButton ph4RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph4RB2);
		EditText ph4year = (EditText) sectionLayout4
				.findViewById(R.id.ph4yearET);
		ph4year.setOnFocusChangeListener(section4ETFocusChangeListner);

		RadioGroup ph5RG = (RadioGroup) sectionLayout4.findViewById(R.id.ph5RG);
		ph5RG.setOnCheckedChangeListener(section4RGChangeListner);
		RadioButton ph5RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph5RB1);
		RadioButton ph5RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph5RB2);
		EditText ph5year = (EditText) sectionLayout4.findViewById(R.id.ph5year);
		ph5year.setOnFocusChangeListener(section4ETFocusChangeListner);

		RadioGroup ph6RG = (RadioGroup) sectionLayout4.findViewById(R.id.ph6RG);
		ph6RG.setOnCheckedChangeListener(section4RGChangeListner);
		RadioButton ph6RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph6RB1);
		RadioButton ph6RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph6RB2);
		EditText ph6year = (EditText) sectionLayout4.findViewById(R.id.ph6year);
		ph6year.setOnFocusChangeListener(section4ETFocusChangeListner);

		RadioGroup ph7RG = (RadioGroup) sectionLayout4.findViewById(R.id.ph7RG);
		ph7RG.setOnCheckedChangeListener(section4RGChangeListner);
		RadioButton ph7RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph7RB1);
		RadioButton ph7RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph7RB2);
		EditText ph7year = (EditText) sectionLayout4.findViewById(R.id.ph7year);
		ph7year.setOnFocusChangeListener(section4ETFocusChangeListner);

		RadioGroup ph8RG = (RadioGroup) sectionLayout4.findViewById(R.id.ph8RG);
		ph8RG.setOnCheckedChangeListener(section4RGChangeListner);
		RadioButton ph8RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph8RB1);
		RadioButton ph8RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph8RB2);
		EditText ph8year = (EditText) sectionLayout4.findViewById(R.id.ph8year);
		ph8year.setOnFocusChangeListener(section4ETFocusChangeListner);

		RadioGroup ph9RG = (RadioGroup) sectionLayout4.findViewById(R.id.ph9RG);
		ph9RG.setOnCheckedChangeListener(section4RGChangeListner);
		RadioButton ph9RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph9RB1);
		RadioButton ph9RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph9RB2);
		EditText ph9year = (EditText) sectionLayout4.findViewById(R.id.ph9year);
		ph9year.setOnFocusChangeListener(section4ETFocusChangeListner);

		NoDefaultSpinner ph9value = (NoDefaultSpinner) sectionLayout4
				.findViewById(R.id.ph9value);

		RadioGroup ph10RG = (RadioGroup) sectionLayout4
				.findViewById(R.id.ph10RG);
		ph10RG.setOnCheckedChangeListener(section4RGChangeListner);
		RadioButton ph10RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph10RB1);
		RadioButton ph10RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph10RB2);
		EditText ph10year = (EditText) sectionLayout4
				.findViewById(R.id.ph10year);
		ph10year.setOnFocusChangeListener(section4ETFocusChangeListner);

		EditText ph10value = (EditText) sectionLayout4
				.findViewById(R.id.ph10value);
		ph10value.setOnFocusChangeListener(section4ETFocusChangeListner);

		RadioGroup ph11RG = (RadioGroup) sectionLayout4
				.findViewById(R.id.ph11RG);
		ph11RG.setOnCheckedChangeListener(section4RGChangeListner);
		RadioButton ph11RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph11RB1);
		RadioButton ph11RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph11RB2);
		EditText ph11year = (EditText) sectionLayout4
				.findViewById(R.id.ph11year);
		ph11year.setOnFocusChangeListener(section4ETFocusChangeListner);

		EditText ph11value = (EditText) sectionLayout4
				.findViewById(R.id.ph11value);
		ph11value.setOnFocusChangeListener(section4ETFocusChangeListner);

		RadioGroup ph12RG = (RadioGroup) sectionLayout4
				.findViewById(R.id.ph12RG);
		ph12RG.setOnCheckedChangeListener(section4RGChangeListner);
		RadioButton ph12RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph12RB1);
		RadioButton ph12RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph12RB2);
		RadioButton ph12RB3 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph12RB3);

		RadioGroup ph13RG = (RadioGroup) sectionLayout4
				.findViewById(R.id.ph13RG);
		ph13RG.setOnCheckedChangeListener(section4RGChangeListner);
		RadioButton ph13RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph13RB1);
		RadioButton ph13RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph13RB2);
		RadioButton ph13RB3 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph13RB3);

		EditText ph14value = (EditText) sectionLayout4
				.findViewById(R.id.ph14value);

		// RadioGroup ph15RG = (RadioGroup)
		// sectionLayout4.findViewById(R.id.ph15RG);
		RadioButton ph15RB1 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph15RB1);
		RadioButton ph15RB2 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph15RB2);
		RadioButton ph15RB3 = (RadioButton) sectionLayout4
				.findViewById(R.id.ph15RB3);

		if (task == STORE) {

			ContentValues cv = new ContentValues();
			cv.put("key", Brave.encryptString(Brave.USER_NAME));

			int ph1RGValue = 0;
			if (ph1RB1.isChecked())
				ph1RGValue = 1;
			else if (ph1RB2.isChecked())
				ph1RGValue = 2;

			cv.put("ph1RG", Brave.encryptInteger(ph1RGValue));
			cv.put("ph1year", Brave.encryptString(ph1year.getText().toString()));
			cv.put("ph1value1",
					Brave.encryptInteger(ph1CB1.isChecked() ? 1 : 2));
			cv.put("ph1value2",
					Brave.encryptInteger(ph1CB2.isChecked() ? 1 : 2));
			cv.put("ph1value3",
					Brave.encryptInteger(ph1CB3.isChecked() ? 1 : 2));
			cv.put("ph1value4",
					Brave.encryptInteger(ph1CB4.isChecked() ? 1 : 2));
			cv.put("ph1value5",
					Brave.encryptInteger(ph1CB5.isChecked() ? 1 : 2));

			int ph2RGValue = 0;
			if (ph2RB1.isChecked())
				ph2RGValue = 1;
			else if (ph2RB2.isChecked())
				ph2RGValue = 2;

			cv.put("ph2RG", Brave.encryptInteger(ph2RGValue));
			cv.put("ph2year", Brave.encryptString(ph2year.getText().toString()));
			cv.put("ph2value1",
					Brave.encryptInteger(ph2CB1.isChecked() ? 1 : 2));
			cv.put("ph2value2",
					Brave.encryptInteger(ph2CB2.isChecked() ? 1 : 2));
			cv.put("ph2value3",
					Brave.encryptInteger(ph2CB3.isChecked() ? 1 : 2));

			int ph3RGValue = 0;
			if (ph3RB1.isChecked())
				ph3RGValue = 1;
			else if (ph3RB2.isChecked())
				ph3RGValue = 2;

			cv.put("ph3RG", Brave.encryptInteger(ph3RGValue));
			cv.put("ph3year", Brave.encryptString(ph3year.getText().toString()));

			int ph4RGValue = 0;
			if (ph4RB1.isChecked())
				ph4RGValue = 1;
			else if (ph4RB2.isChecked())
				ph4RGValue = 2;

			cv.put("ph4RG", Brave.encryptInteger(ph4RGValue));
			cv.put("ph4year", Brave.encryptString(ph4year.getText().toString()));

			int ph5RGValue = 0;
			if (ph5RB1.isChecked())
				ph5RGValue = 1;
			else if (ph5RB2.isChecked())
				ph5RGValue = 2;

			cv.put("ph5RG", Brave.encryptInteger(ph5RGValue));
			cv.put("ph5year", Brave.encryptString(ph5year.getText().toString()));

			int ph6RGValue = 0;
			if (ph6RB1.isChecked())
				ph6RGValue = 1;
			else if (ph6RB2.isChecked())
				ph6RGValue = 2;

			cv.put("ph6RG", Brave.encryptInteger(ph6RGValue));
			cv.put("ph6year", Brave.encryptString(ph6year.getText().toString()));

			int ph7RGValue = 0;
			if (ph7RB1.isChecked())
				ph7RGValue = 1;
			else if (ph7RB2.isChecked())
				ph7RGValue = 2;

			cv.put("ph7RG", Brave.encryptInteger(ph7RGValue));
			cv.put("ph7year", Brave.encryptString(ph7year.getText().toString()));

			int ph8RGValue = 0;
			if (ph8RB1.isChecked())
				ph8RGValue = 1;
			else if (ph8RB2.isChecked())
				ph8RGValue = 2;

			cv.put("ph8RG", Brave.encryptInteger(ph8RGValue));
			cv.put("ph8year", Brave.encryptString(ph8year.getText().toString()));

			int ph9RGValue = 0;
			if (ph9RB1.isChecked())
				ph9RGValue = 1;
			else if (ph9RB2.isChecked())
				ph9RGValue = 2;

			cv.put("ph9RG", Brave.encryptInteger(ph9RGValue));
			cv.put("ph9year", Brave.encryptString(ph9year.getText().toString()));
			cv.put("ph9value",
					Brave.encryptInteger(ph9value.getSelectedItemPosition()));

			int ph10RGValue = 0;
			if (ph10RB1.isChecked())
				ph10RGValue = 1;
			else if (ph10RB2.isChecked())
				ph10RGValue = 2;

			cv.put("ph10RG", Brave.encryptInteger(ph10RGValue));
			cv.put("ph10year",
					Brave.encryptString(ph10year.getText().toString()));
			cv.put("ph10value",
					Brave.encryptString(ph10value.getText().toString()));

			int ph11RGValue = 0;
			if (ph11RB1.isChecked())
				ph11RGValue = 1;
			else if (ph11RB2.isChecked())
				ph11RGValue = 2;

			cv.put("ph11RG", Brave.encryptInteger(ph11RGValue));
			cv.put("ph11year",
					Brave.encryptString(ph11year.getText().toString()));
			cv.put("ph11value",
					Brave.encryptString(ph11value.getText().toString()));

			int ph12RGValue = 0;
			if (ph12RB1.isChecked())
				ph12RGValue = 1;
			else if (ph12RB2.isChecked())
				ph12RGValue = 2;
			else if (ph12RB3.isChecked())
				ph12RGValue = 3;

			cv.put("ph12value", Brave.encryptInteger(ph12RGValue));

			int ph13RGValue = 0;
			if (ph13RB1.isChecked())
				ph13RGValue = 1;
			else if (ph13RB2.isChecked())
				ph13RGValue = 2;
			else if (ph13RB3.isChecked())
				ph13RGValue = 3;

			cv.put("ph13value", Brave.encryptInteger(ph13RGValue));
			cv.put("ph14value",
					Brave.encryptString(ph14value.getText().toString()));
			int ph15RGValue = 0;
			if (ph15RB1.isChecked())
				ph15RGValue = 1;
			else if (ph15RB2.isChecked())
				ph15RGValue = 2;
			else if (ph15RB3.isChecked())
				ph15RGValue = 3;

			cv.put("ph15value", Brave.encryptInteger(ph15RGValue));
			Brave.getDatabaseManager().addSection4DataIntoDB(cv,
					Brave.USER_NAME);

			taskCompleted = true;
		} else if (task == SHOW) {
			titleTV.setText(R.string.past_medical_history);
			Section4 section4 = Brave.getDatabaseManager().getSection4Data(
					Brave.USER_NAME);
			if (section4 != null) {
				Log.e("", "section4 not null");
				/*---------PH1-----------*/
				if (section4.getPh1RG() == 1) {
					ph1RB1.setChecked(true);
					ph1year.setEnabled(true);
					ph1year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg));
					if (section4.getPh1year() > 0)
						ph1year.setText("" + section4.getPh1year());
				} else if (section4.getPh1RG() == 2) {
					ph1RB2.setChecked(true);
					ph1year.setEnabled(false);
					ph1year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg_disable));
				}
				if (section4.getPh1value1() == 1)
					ph1CB1.setChecked(true);
				else
					ph1CB1.setChecked(false);

				if (section4.getPh1value2() == 1)
					ph1CB2.setChecked(true);
				else
					ph1CB2.setChecked(false);

				if (section4.getPh1value3() == 1)
					ph1CB3.setChecked(true);
				else
					ph1CB3.setChecked(false);

				if (section4.getPh1value4() == 1)
					ph1CB4.setChecked(true);
				else
					ph1CB4.setChecked(false);

				if (section4.getPh1value5() == 1)
					ph1CB5.setChecked(true);
				else
					ph1CB5.setChecked(false);

				/*---------PH2-----------*/
				if (section4.getPh2RG() == 1) {
					ph2RB1.setChecked(true);
					ph2year.setEnabled(true);
					ph2year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg));
					if (section4.getPh2year() > 0)
						ph2year.setText("" + section4.getPh2year());
				} else if (section4.getPh2RG() == 2) {
					ph2RB2.setChecked(true);
					ph2year.setEnabled(false);
					ph2year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg_disable));
				}

				if (section4.getPh2value1() == 1)
					ph2CB1.setChecked(true);
				else
					ph2CB1.setChecked(false);

				if (section4.getPh2value2() == 1)
					ph2CB2.setChecked(true);
				else
					ph2CB2.setChecked(false);

				if (section4.getPh2value3() == 1)
					ph2CB3.setChecked(true);
				else
					ph2CB3.setChecked(false);

				/*-----------------PH3--------------------*/
				if (section4.getPh3RG() == 1) {
					ph3RB1.setChecked(true);
					ph3year.setEnabled(true);
					ph3year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg));
					if (section4.getPh3year() > 0)
						ph3year.setText("" + section4.getPh3year());
				} else if (section4.getPh3RG() == 2) {
					ph3RB2.setChecked(true);
					ph3year.setEnabled(false);
					ph3year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg_disable));
				}
				/*-----------------PH4--------------------*/
				if (section4.getPh4RG() == 1) {
					ph4RB1.setChecked(true);
					ph4year.setEnabled(true);
					ph4year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg));
					if (section4.getPh4year() > 0)
						ph4year.setText("" + section4.getPh4year());
				} else if (section4.getPh4RG() == 2) {
					ph4RB2.setChecked(true);
					ph4year.setEnabled(false);
					ph4year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg_disable));
				}
				/*-----------------PH5--------------------*/
				if (section4.getPh5RG() == 1) {
					ph5RB1.setChecked(true);
					ph5year.setEnabled(true);
					ph5year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg));
					if (section4.getPh5year() > 0)
						ph5year.setText("" + section4.getPh5year());
				} else if (section4.getPh5RG() == 2) {
					ph5RB2.setChecked(true);
					ph5year.setEnabled(false);
					ph5year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg_disable));
				}
				/*--------------PH6------------*/
				if (section4.getPh6RG() == 1) {
					ph6RB1.setChecked(true);
					ph6year.setEnabled(true);
					ph6year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg));
					if (section4.getPh6year() > 0)
						ph6year.setText("" + section4.getPh6year());
				} else if (section4.getPh6RG() == 2) {
					ph6RB2.setChecked(true);
					ph6year.setEnabled(false);
					ph6year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg_disable));
				}

				/*-----------------PH7---------------*/
				if (section4.getPh7RG() == 1) {
					ph7RB1.setChecked(true);
					ph7year.setEnabled(true);
					ph7year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg));
					if (section4.getPh7year() > 0)
						ph7year.setText("" + section4.getPh7year());
				} else if (section4.getPh7RG() == 2) {
					ph7RB2.setChecked(true);
					ph7year.setEnabled(false);
					ph7year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg_disable));
				}
				/*------------------PH8---------------*/
				if (section4.getPh8RG() == 1) {
					ph8RB1.setChecked(true);
					ph8year.setEnabled(true);
					ph8year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg));
					if (section4.getPh8year() > 0)
						ph8year.setText("" + section4.getPh8year());
				} else if (section4.getPh8RG() == 2) {
					ph8RB2.setChecked(true);
					ph8year.setEnabled(false);
					ph8year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg_disable));
				}

				/*------------------PH9---------------*/
				if (section4.getPh9RG() == 1) {
					ph9RB1.setChecked(true);
					ph9year.setEnabled(true);
					ph9year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg));
					if (section4.getPh9year() > 0)
						ph9year.setText("" + section4.getPh9year());
				} else if (section4.getPh9RG() == 2) {
					ph9RB2.setChecked(true);
					ph9year.setEnabled(false);
					ph9year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg_disable));
				}

				ph9value.setSelection(section4.getPh9value());

				/*------------------PH10---------------*/
				if (section4.getPh10RG() == 1) {
					ph10RB1.setChecked(true);
					ph10year.setEnabled(true);
					ph10year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg));
					if (section4.getPh10year() > 0)
						ph10year.setText("" + section4.getPh10year());
				} else if (section4.getPh10RG() == 2) {
					ph10RB2.setChecked(true);
					ph10year.setEnabled(false);
					ph10year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg_disable));
				}

				ph10value.setText("" + section4.getPh10value());

				/*------------------PH11---------------*/
				if (section4.getPh11RG() == 1) {
					ph11RB1.setChecked(true);
					ph11year.setEnabled(true);
					ph11year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg));
					if (section4.getPh11year() > 0)
						ph11year.setText("" + section4.getPh11year());
				} else if (section4.getPh11RG() == 2) {
					ph11RB2.setChecked(true);
					ph11year.setEnabled(false);
					ph11year.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.edittext_bg_disable));
				}

				ph11value.setText("" + section4.getPh11value());

				if (section4.getPh12value() == 1)
					ph12RB1.setChecked(true);
				else if (section4.getPh12value() == 2)
					ph12RB2.setChecked(true);
				else if (section4.getPh12value() == 3)
					ph12RB3.setChecked(true);

				if (section4.getPh13value() == 1)
					ph13RB1.setChecked(true);
				else if (section4.getPh13value() == 2)
					ph13RB2.setChecked(true);
				else if (section4.getPh13value() == 3)
					ph13RB3.setChecked(true);

				if (section4.getPh14value() > 0)
					ph14value.setText("" + section4.getPh14value());

				if (section4.getPh15value() == 1)
					ph15RB1.setChecked(true);
				else if (section4.getPh15value() == 2)
					ph15RB2.setChecked(true);
				else if (section4.getPh15value() == 3)
					ph15RB3.setChecked(true);
			}
			taskCompleted = true;
		}
		return taskCompleted;
	}

	private RadioGroup.OnCheckedChangeListener section3RGChangeListner = new RadioGroup.OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			switch (checkedId) {
			case R.id.SSO11Btn1:
				EditText et = (EditText) findViewById(R.id.SSO12);
				et.setEnabled(true);
				et.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				et = (EditText) findViewById(R.id.SSO13);
				et.setEnabled(true);
				et.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg));
				break;
			case R.id.SSO11Btn2:
				et = (EditText) findViewById(R.id.SSO12);
				et.getText().clear();
				et.setEnabled(false);
				et.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				et = (EditText) findViewById(R.id.SSO13);
				et.getText().clear();
				et.setEnabled(false);
				et.setBackgroundDrawable(getResources().getDrawable(
						R.drawable.edittext_bg_disable));
				break;
			default:
				break;
			}
		}

	};

	private RadioGroup.OnCheckedChangeListener infoRGChangeListner = new RadioGroup.OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			NoDefaultSpinner infoReasonForIncomplete = (NoDefaultSpinner) findViewById(R.id.infoReasonForIncomplete);
			switch (checkedId) {
			case R.id.infoDataColCompleteRB:
				infoReasonForIncomplete.setSelected(false);
				infoReasonForIncomplete.setEnabled(false);
				infoReasonForIncomplete.setFocusable(false);
				break;
			case R.id.infoDataColIncompleteRB:
				infoReasonForIncomplete.setEnabled(true);
				infoReasonForIncomplete.setFocusable(true);				
				break;
			default:
				break;
			}
		}

	};
	
	private boolean storeFetchSection3Data(int task) {
		// TODO Auto-generated method stub
		boolean taskCompleted = false;
		Button SSO1Date = (Button) sectionLayout3.findViewById(R.id.SSO1Date);
		SSO1Date.setOnClickListener(this);

		Button SSO1Time = (Button) sectionLayout3.findViewById(R.id.SSO1Time);
		SSO1Time.setOnClickListener(this);

		Button SSO2Date = (Button) sectionLayout3.findViewById(R.id.SSO2Date);
		SSO2Date.setOnClickListener(this);

		Button SSO2Time = (Button) sectionLayout3.findViewById(R.id.SSO2Time);
		SSO2Time.setOnClickListener(this);

		NoDefaultSpinner SSO3 = (NoDefaultSpinner) sectionLayout3
				.findViewById(R.id.SSO3);
		NoDefaultSpinner SSO4 = (NoDefaultSpinner) sectionLayout3
				.findViewById(R.id.SSO4);
		NoDefaultSpinner SSO5 = (NoDefaultSpinner) sectionLayout3
				.findViewById(R.id.SSO5);

		Button SSO6 = (Button) sectionLayout3.findViewById(R.id.SSO6);
		SSO6.setOnClickListener(this);

		NoDefaultSpinner SSO7 = (NoDefaultSpinner) sectionLayout3
				.findViewById(R.id.SSO7);
		NoDefaultSpinner SSO8 = (NoDefaultSpinner) sectionLayout3
				.findViewById(R.id.SSO8);
		NoDefaultSpinner SSO9 = (NoDefaultSpinner) sectionLayout3
				.findViewById(R.id.SSO9);
		EditText SSO10 = (EditText) sectionLayout3.findViewById(R.id.SSO10);
		SSO10.setOnFocusChangeListener(section3ETFocusChangeListner);
		RadioGroup SSO11 = (RadioGroup) findViewById(R.id.SSO11);
		SSO11.setOnCheckedChangeListener(section3RGChangeListner);
		RadioButton SSO11Btn1 = (RadioButton) sectionLayout3
				.findViewById(R.id.SSO11Btn1);
		RadioButton SSO11Btn2 = (RadioButton) sectionLayout3
				.findViewById(R.id.SSO11Btn2);
		EditText SSO12 = (EditText) sectionLayout3.findViewById(R.id.SSO12);
		SSO12.setOnFocusChangeListener(section3ETFocusChangeListner);
		EditText SSO13 = (EditText) sectionLayout3.findViewById(R.id.SSO13);
		SSO13.setOnFocusChangeListener(section3ETFocusChangeListner);
		EditText SSO14 = (EditText) sectionLayout3.findViewById(R.id.SSO14);
		SSO14.setOnFocusChangeListener(section3ETFocusChangeListner);
		EditText SSO15 = (EditText) sectionLayout3.findViewById(R.id.SSO15);
		SSO15.setOnFocusChangeListener(section3ETFocusChangeListner);
		EditText SSO16 = (EditText) sectionLayout3.findViewById(R.id.SSO16);
		SSO16.setOnFocusChangeListener(section3ETFocusChangeListner);
		NoDefaultSpinner SSO17 = (NoDefaultSpinner) sectionLayout3
				.findViewById(R.id.SSO17);

		if (task == STORE) {
			ContentValues cv = new ContentValues();
			cv.put("key", Brave.encryptString(Brave.USER_NAME));
			cv.put("SSO1Date",
					Brave.encryptString(SSO1Date.getText().toString()));
			cv.put("SSO1Time",
					Brave.encryptString(SSO1Time.getText().toString()));
			cv.put("SSO2Date",
					Brave.encryptString(SSO2Date.getText().toString()));
			cv.put("SSO2Time",
					Brave.encryptString(SSO2Time.getText().toString()));
			cv.put("SSO3",
					Brave.encryptInteger((SSO3.getSelectedItemPosition())));
			cv.put("SSO4", Brave.encryptInteger(SSO4.getSelectedItemPosition()));
			cv.put("SSO5", Brave.encryptInteger(SSO5.getSelectedItemPosition()));
			cv.put("SSO6", Brave.encryptString(SSO6.getText().toString()));
			cv.put("SSO7", Brave.encryptInteger(SSO7.getSelectedItemPosition()));
			cv.put("SSO8", Brave.encryptInteger(SSO8.getSelectedItemPosition()));
			cv.put("SSO9", Brave.encryptInteger(SSO9.getSelectedItemPosition()));
			cv.put("SSO10", Brave.encryptString(SSO10.getText().toString()));
			int SSO11Value = 0;
			if (SSO11Btn1.isChecked())
				SSO11Value = 1;
			else if (SSO11Btn2.isChecked())
				SSO11Value = 2;
			cv.put("SSO11", Brave.encryptInteger(SSO11Value));
			cv.put("SSO12", Brave.encryptString(SSO12.getText().toString()));
			cv.put("SSO13", Brave.encryptString(SSO13.getText().toString()));
			cv.put("SSO14", Brave.encryptString(SSO14.getText().toString()));
			cv.put("SSO15", Brave.encryptString(SSO15.getText().toString()));
			cv.put("SSO16", Brave.encryptString(SSO16.getText().toString()));
			cv.put("SSO17",
					Brave.encryptInteger(SSO17.getSelectedItemPosition()));

			Brave.getDatabaseManager().addSection3DataIntoDB(cv,
					Brave.USER_NAME);

			taskCompleted = true;
		} else if (task == SHOW) {
			titleTV.setText(R.string.signs_and_symptoms_onset);
			Section3 section3 = Brave.getDatabaseManager().getSection3Data(
					Brave.USER_NAME);
			if (section3 != null) {
				Log.e("", "section3 not null");
				if (section3.getSSO1Date() != null
						&& section3.getSSO1Date().length() > 0) {
					SSO1Date.setText(section3.getSSO1Date());
				} else {
					SSO1Date.setText(day + "-" + (month + 1) + "-" + year);
				}
				if (section3.getSSO1Time() != null
						&& section3.getSSO1Time().length() > 0) {
					SSO1Time.setText(section3.getSSO1Time());
				} else {
					SSO1Time.setText(hour + ":" + minute);
				}
				if (section3.getSSO2Date() != null
						&& section3.getSSO2Date().length() > 0) {
					SSO2Date.setText(section3.getSSO2Date());
				} else {
					SSO2Date.setText(day + "-" + (month + 1) + "-" + year);
				}
				if (section3.getSSO2Time() != null
						&& section3.getSSO2Time().length() > 0) {
					SSO2Time.setText(section3.getSSO2Time());
				} else {
					SSO2Time.setText(hour + ":" + minute);
				}
				SSO3.setSelection(section3.getSSO3());
				SSO4.setSelection(section3.getSSO4());
				SSO5.setSelection(section3.getSSO5());

				if (section3.getSSO6() != null
						&& section3.getSSO6().length() > 0) {
					SSO6.setText(section3.getSSO6());
				} else {
					SSO6.setText(hour + ":" + minute);
				}

				SSO7.setSelection(section3.getSSO7());
				SSO8.setSelection(section3.getSSO8());
				SSO9.setSelection(section3.getSSO9());
				if (section3.getSSO10() > 0)
					SSO10.setText("" + section3.getSSO10());
				if (section3.getSSO11() == 1) {
					SSO11Btn1.setChecked(true);
				} else if (section3.getSSO11() == 2) {
					SSO11Btn2.setChecked(true);
				}
				if (section3.getSSO12().length() > 0)
					SSO12.setText("" + section3.getSSO12());
				if (section3.getSSO13() > 0)
					SSO13.setText("" + section3.getSSO13());
				if (section3.getSSO14() > 0)
					SSO14.setText("" + section3.getSSO14());
				if (section3.getSSO15() > 0)
					SSO15.setText("" + section3.getSSO15());
				if (section3.getSSO16() > 0)
					SSO16.setText("" + section3.getSSO16());
				SSO17.setSelection(section3.getSSO17());
			} else {
				// if no data from db then set default date and time
				SSO1Date.setText(day + "-" + (month + 1) + "-" + year);
				SSO1Time.setText(hour + ":" + minute);
				SSO2Date.setText(day + "-" + (month + 1) + "-" + year);
				SSO2Time.setText(hour + ":" + minute);
				SSO6.setText(hour + ":" + minute);
			}
			taskCompleted = true;
		}
		return taskCompleted;
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		storeDataIntoDB(currently_selected_layout, STORE);
		super.onBackPressed();
	}
	
	public void save(){
		storeDataIntoDB(currently_selected_layout, STORE);
	}

	private boolean storeFetchSection2Data(int task) {
		// TODO Auto-generated method stub

		boolean taskCompleted = false;
		NoDefaultSpinner d1 = (NoDefaultSpinner) sectionLayout2
				.findViewById(R.id.d1);
		NoDefaultSpinner d2 = (NoDefaultSpinner) sectionLayout2
				.findViewById(R.id.d2);
		Button d3 = (Button) sectionLayout2.findViewById(R.id.d3);
		d3.setOnClickListener(this);

		EditText d4 = (EditText) sectionLayout2.findViewById(R.id.d4);
		d4.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if (!hasFocus) {

					if (((EditText) v).getText().length() > 0) {
						long value = Long.valueOf(((EditText) v).getText()
								.toString());
						if (value < 1 || value > 120) {
							((EditText) v).getText().clear();
							openErrorDialog("Age");
							v.requestFocus();
						}
					}
				}
			}
		});

		NoDefaultSpinner d5 = (NoDefaultSpinner) sectionLayout2
				.findViewById(R.id.d5);
		NoDefaultSpinner d6 = (NoDefaultSpinner) sectionLayout2
				.findViewById(R.id.d6);

		if (task == STORE) {
			ContentValues cv = new ContentValues();
			cv.put("key", Brave.encryptString(Brave.USER_NAME));
			cv.put("d1", Brave.encryptInteger(d1.getSelectedItemPosition()));
			cv.put("d2", Brave.encryptInteger(d2.getSelectedItemPosition()));
			cv.put("d3", Brave.encryptString(d3.getText().toString()));

			if (d4.getText().length() > 0) {
				if (Long.valueOf(d4.getText().toString()) < 1
						|| Long.valueOf(d4.getText().toString()) > 120) {
					d4.requestFocus();
					d4.getText().clear();
					openErrorDialog("Age");
					return false;
				}
			}
			if (d4.getText() != null && d4.getText().length() > 0) {
				Brave.D4_value = Integer.parseInt(d4.getText().toString());
			}
			cv.put("d4", Brave.encryptString(d4.getText().toString()));
			cv.put("d5", Brave.encryptInteger(d5.getSelectedItemPosition()));
			cv.put("d6", Brave.encryptInteger(d6.getSelectedItemPosition()));
			Brave.getDatabaseManager().addSection2DataIntoDB(cv,
					Brave.USER_NAME);
			taskCompleted = true;
		} else if (task == SHOW) {
			titleTV.setText(R.string.demographics);
			Section2 section2 = Brave.getDatabaseManager().getSection2Data(
					Brave.USER_NAME);
			if (section2 != null) {
				Log.e("", "section2 not null");
				d1.setSelection(section2.getD1());
				d2.setSelection(section2.getD2());
				if (section2.getD3() != null && section2.getD3().length() > 0) {
					d3.setText(section2.getD3());
				} else {
					d3.setText(day + "-" + (month + 1) + "-" + year);
				}
				d4.setText("" + section2.getD4());
				d5.setSelection(section2.getD5());
				d6.setSelection(section2.getD6());
			} else {
				d3.setText(day + "-" + (month + 1) + "-" + year);
			}
			taskCompleted = true;
		}
		return taskCompleted;
	}

	private void addViewIntoSectionContainer(int layoutId) {

		currently_selected_layout = layoutId;
		sectionContainer.removeAllViews();
		
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		layoutParams.weight = 1;
		if (layoutId == R.layout.information_layout) {
			information_layout.setLayoutParams(layoutParams);
			sectionContainer.addView(information_layout);
		}else if (layoutId == R.layout.section1_layout) {
			sectionLayout1.setLayoutParams(layoutParams);
			sectionContainer.addView(sectionLayout1);
		} else if (layoutId == R.layout.section2_layout) {
			sectionLayout2.setLayoutParams(layoutParams);
			sectionContainer.addView(sectionLayout2);
		} else if (layoutId == R.layout.section3_layout) {
			sectionLayout3.setLayoutParams(layoutParams);
			sectionContainer.addView(sectionLayout3);
		} else if (layoutId == R.layout.section4_layout) {
			sectionLayout4.setLayoutParams(layoutParams);
			sectionContainer.addView(sectionLayout4);
		}
		storeDataIntoDB(layoutId, SHOW);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		currentButtonId = v.getId();
		Button btn = (Button) findViewById(currentButtonId);
		String text = btn.getText().toString();
		
		switch (currentButtonId) {
		
		case R.id.d3:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;

		case R.id.SSO1Date:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;

		case R.id.SSO2Date:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;

		case R.id.SSO1Time:
			getCurrentTime(text);
			showDialog(TIME_DIALOG_ID);
			break;

		case R.id.SSO2Time:
			getCurrentTime(text);
			showDialog(TIME_DIALOG_ID);
			break;

		case R.id.SSO6:
			getCurrentTime(text);
			showDialog(TIME_DIALOG_ID);
			break;
			
		case R.id.dateOfEnrolmentButton:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;
			
		case R.id.dateOfInterview:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;
			
		case R.id.dateOfSampling:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;
			
		case R.id.dateOfLastMeal:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;
			
		case R.id.endDate:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;
	
		case R.id.endTime:
			getCurrentTime(text);
			showDialog(TIME_DIALOG_ID);
			break;
			
		case R.id.startTime:
			getCurrentTime(text);
			showDialog(TIME_DIALOG_ID);
			break;
			
		case R.id.timeOfSampling:
			getCurrentTime(text);
			showDialog(TIME_DIALOG_ID);
			break;
			
		case R.id.timeOfLastMeal:
			getCurrentTime(text);
			showDialog(TIME_DIALOG_ID);
			break;
		default:
			break;
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, datePickerListener, currentYear,
					currentMonth, currentDay);
		case TIME_DIALOG_ID:
			return new TimePickerDialog(this, timePickerListener, currentHour,
					currentMin, true);
		}
		return null;
	}
	
	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {

			String date = selectedDay + "-" + (selectedMonth + 1) + "-" +selectedYear;
			Button button = (Button) findViewById(currentButtonId);
			if(currentButtonId != R.id.dateOfInterview){
				
				button.setText(date);
			}

			if(currentButtonId == R.id.dateOfEnrolmentButton){
				Button interviewBTN = (Button) findViewById(R.id.dateOfInterview);				
				getCurrentDate(interviewBTN.getText().toString());
				if(currentYear < selectedYear){
					interviewBTN.setText(date);
				}else if(currentYear == selectedYear){
					if(currentMonth < selectedMonth){
						interviewBTN.setText(date);
					}else if(currentMonth == selectedMonth){
						if(currentDay < selectedDay){
							interviewBTN.setText(date);
						}
					}
				}				
			}
			else if(currentButtonId == R.id.dateOfInterview){
				Button enrolmentBTN = (Button) findViewById(R.id.dateOfEnrolmentButton);				
				getCurrentDate(enrolmentBTN.getText().toString());
				String enrolmentDate = currentDay + "-" + (currentMonth + 1) + "-" +currentYear;				
				boolean wrongDate = false;
				
				if(currentYear > selectedYear){
					wrongDate = true;
					button.setText(enrolmentDate);
				}else if(currentYear == selectedYear){
					if(currentMonth > selectedMonth){
						wrongDate = true;
						button.setText(enrolmentDate);
					}else if(currentMonth == selectedMonth){
						if(currentDay > selectedDay){
							wrongDate = true;
							button.setText(enrolmentDate);
						}
					}
				}
				
				if(!wrongDate)
					button.setText(date);
			}
		}
	};

	private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int selectedHour,
				int selectedMinute) {
			hour = selectedHour;
			minute = selectedMinute;
			String time = hour + ":" + minute;
			Button button = (Button) findViewById(currentButtonId);
			
			if(currentButtonId != R.id.endTime)
				button.setText(time);

			if(currentButtonId == R.id.endTime){
				Button startTimeBTN = (Button) findViewById(R.id.startTime);
				getCurrentTime(startTimeBTN.getText().toString());
				String newTime = currentHour + ":" + currentMin;
				if(selectedHour < currentHour){
					button.setText(newTime);
				}else if(selectedHour == currentHour){
					if(selectedMinute < currentMin){
						button.setText(newTime);
					}
				}
			}else if(currentButtonId == R.id.startTime){
				Button endTimeBTN = (Button) findViewById(R.id.endTime);
				getCurrentTime(endTimeBTN.getText().toString());
//				String newTime = currentHour + ":" + currentMin;
				if(selectedHour > currentHour){
					endTimeBTN.setText(time);
				}else if(selectedHour == currentHour){
					if(selectedMinute > currentMin){
						endTimeBTN.setText(time);
					}
				}
			}
		}
	};
	
	private View.OnFocusChangeListener section4ETFocusChangeListner = new View.OnFocusChangeListener() {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub

			try{
				if (!hasFocus) {
					sec4nextFocusPoint = false;
					sec4viewId = 0;
					long value;
					switch (v.getId()) {
					case R.id.ph1yearET:
						EditText et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {

							value = Long.valueOf(et.getText().toString());
							if (value < 1 || value > Brave.D4_value) {
								sec4nextFocusPoint = true;
								sec4viewId = R.id.ph1yearET;
								et.getText().clear();
								openErrorDialog("Age on diagnosis");
							}
						}
						break;
					case R.id.ph2yearET:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 1 || value > Brave.D4_value) {
								sec4nextFocusPoint = true;
								sec4viewId = R.id.ph2yearET;
								et.getText().clear();
								openErrorDialog("Age on diagnosis");
							}
						}
						break;
					case R.id.ph3yearET:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 1 || value > Brave.D4_value) {
								sec4nextFocusPoint = true;
								sec4viewId = R.id.ph3yearET;
								et.getText().clear();
								openErrorDialog("Age on diagnosis");
							}
						}
						break;
					case R.id.ph4yearET:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 1 || value > Brave.D4_value) {
								sec4nextFocusPoint = true;
								sec4viewId = R.id.ph4yearET;
								et.getText().clear();
								openErrorDialog("Age on diagnosis");
							}
						}
						break;
					case R.id.ph5year:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 1 || value > Brave.D4_value) {
								sec4nextFocusPoint = true;
								sec4viewId = R.id.ph5year;
								et.getText().clear();
								openErrorDialog("Age on diagnosis");
							}
						}
						break;
					case R.id.ph6year:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 1 || value > Brave.D4_value) {
								sec4nextFocusPoint = true;
								sec4viewId = R.id.ph6year;
								et.getText().clear();
								openErrorDialog("Age on diagnosis");
							}
						}
						break;
					case R.id.ph7year:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 1 || value > Brave.D4_value) {
								sec4nextFocusPoint = true;
								sec4viewId = R.id.ph7year;
								et.getText().clear();
								openErrorDialog("Age on diagnosis");
							}
						}
						break;
					case R.id.ph8year:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 1 || value > Brave.D4_value) {
								sec4nextFocusPoint = true;
								sec4viewId = R.id.ph8year;
								et.getText().clear();
								openErrorDialog("Age on diagnosis");
							}
						}
						break;
					case R.id.ph9year:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 1 || value > Brave.D4_value) {
								sec4nextFocusPoint = true;
								sec4viewId = R.id.ph9year;
								et.getText().clear();
								openErrorDialog("Age on diagnosis");
							}
						}
						break;
					case R.id.ph10year:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 1 || value > Brave.D4_value) {
								sec4nextFocusPoint = true;
								sec4viewId = R.id.ph10year;
								et.getText().clear();
								openErrorDialog("Age on diagnosis");
							}
						}
						break;

					case R.id.ph11year:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 1 || value > Brave.D4_value) {
								sec4nextFocusPoint = true;
								sec4viewId = R.id.ph11year;
								et.getText().clear();
								openErrorDialog("Age on diagnosis");
							}
						}
						break;
					default:
						break;
					}
				} else {
					if (sec4nextFocusPoint) {
						if (sec4viewId != 0) {
							try {
								View prev = findViewById(sec4viewId);
								prev.requestFocus();
								if (v.hasFocus()) {
									v.clearFocus();
								}
								if (v instanceof EditText) {
									((EditText) v).setCursorVisible(false);
									v.invalidate();
								}
							} catch (Exception ex) {}
						}
					} else {
						if (v instanceof EditText) {
							((EditText) v).setCursorVisible(true);
							v.invalidate();
						}
					}
				}
			}catch(Exception e){
				Log.e("section4ETFocusChangeListner", e.toString());
			}
		}
	};

	
	
	private View.OnFocusChangeListener infoETFocusChangeListner = new View.OnFocusChangeListener() { 

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			try{
				if (!hasFocus) {
					infoNextFocusPoint = false;
					infoViewId = 0;
					
					switch (v.getId()) {
					case R.id.infoAorB:
						EditText et = (EditText) findViewById(v.getId());
						
						if (et.getText().length() > 0) {
							if(et.getText().toString().equals("A") || et.getText().toString().equals("B") ||
									et.getText().toString().equals("")){

							}else{
								infoNextFocusPoint = true;
								infoViewId = R.id.infoAorB;
								et.getText().clear();
								openErrorDialog("Wrong id");
							}
						}
						break;
					default:
						break;
					}
				} else {
					if (infoNextFocusPoint) {
						if (infoViewId != 0) {
							try {
								View prev = findViewById(infoViewId);
								prev.requestFocus();
								if (v.hasFocus()) {
									v.clearFocus();
								}
								if (v instanceof EditText) {
									((EditText) v).setCursorVisible(false);
									v.invalidate();
								}
							} catch (Exception ex) {}
						}
					} else {
						if (v instanceof EditText) {
							((EditText) v).setCursorVisible(true);
							v.invalidate();
						}
					}
				}

			}catch(Exception e){
				Log.e("infoETFocusChangeListner", e.toString());
			}
			
		}
	};
	
	
	private View.OnFocusChangeListener section3ETFocusChangeListner = new View.OnFocusChangeListener() {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			try{
				if (!hasFocus) {
					sec3nextFocusPoint = false;
					sec3viewId = 0;
					long value;
					switch (v.getId()) {
					case R.id.SSO14:
						EditText et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 0 || value > 99999) {
								sec3nextFocusPoint = true;
								sec3viewId = R.id.SSO14;
								et.getText().clear();
								openErrorDialog("Treatment cost");

							}
						}
						break;
					case R.id.SSO15:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 0 || value > 99999) {
								sec3nextFocusPoint = true;
								sec3viewId = R.id.SSO15;
								et.getText().clear();
								openErrorDialog("Transportation cost");
							}
						}
						break;
					case R.id.SSO16:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 0 || value > 99999) {
								sec3nextFocusPoint = true;
								sec3viewId = R.id.SSO16;
								et.getText().clear();
								openErrorDialog("Accompanying cost");

							}
						}

						break;

					default:
						break;
					}
				} else {
					if (sec3nextFocusPoint) {
						if (sec3viewId != 0) {
							try {
								View prev = findViewById(sec3viewId);
								prev.requestFocus();
								if (v.hasFocus()) {
									v.clearFocus();
								}
								if (v instanceof EditText) {
									((EditText) v).setCursorVisible(false);
									v.invalidate();
								}
							} catch (Exception ex) {}
						}
					} else {
						if (v instanceof EditText) {
							((EditText) v).setCursorVisible(true);
							v.invalidate();
						}
					}
				}
			}catch(Exception e){
				Log.e("section3ETFocusChangeListner", e.toString());
			}
		}
	};

	private View.OnFocusChangeListener section1ETFocusChangeListner = new View.OnFocusChangeListener() {

		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			try{
				if (!hasFocus) {
					
					sec1nextFocus = false;
					sec1viewId = 0;
					long value;
					switch (v.getId()) {
					case R.id.systolicBloodPressure1:
						EditText et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 70 || value > 260) {
								sec1nextFocus = true;
								sec1viewId = R.id.systolicBloodPressure1;
								et.getText().clear();
								openErrorDialog("Systolic blood pressure");
							}
						}
						break;
					case R.id.systolicBloodPressure2:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 70 || value > 260) {
								sec1nextFocus = true;
								sec1viewId = R.id.systolicBloodPressure2;
								et.getText().clear();
								openErrorDialog("Systolic blood pressure 2");

							}
						}
						break;

					case R.id.diastolicBloodPressure1:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 30 || value > 150) {
								sec1nextFocus = true;
								sec1viewId = R.id.diastolicBloodPressure1;
								et.getText().clear();
								openErrorDialog("Diastolic blood pressure");

							}
						}
						break;
					case R.id.diastolicBloodPressure2:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 30 || value > 150) {
								sec1nextFocus = true;
								sec1viewId = R.id.diastolicBloodPressure2;
								et.getText().clear();
								openErrorDialog("Diastolic blood pressure 2");

							}
						}
						break;

					case R.id.height:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 130 || value > 220) {
								sec1nextFocus = true;
								sec1viewId = R.id.height;
								et.getText().clear();
								openErrorDialog("Height");

							}
						}
						break;

					case R.id.wrist:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 70 || value > 130) {
								sec1nextFocus = true;
								sec1viewId = R.id.wrist;
								et.getText().clear();
								openErrorDialog("Wrist");

							}
						}
						break;

					case R.id.hip:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 70 || value > 130) {
								sec1nextFocus = true;
								sec1viewId = R.id.hip;
								et.getText().clear();
								openErrorDialog("Hip");

							}
						}
						break;

					case R.id.weight:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							value = Long.valueOf(et.getText().toString());
							if (value < 40 || value > 150) {
								sec1nextFocus = true;
								sec1viewId = R.id.weight;
								et.getText().clear();
								openErrorDialog("Weight");
							}
						}
						break;

					case R.id.heart_rate:
						et = (EditText) findViewById(v.getId());
						if (et.getText().length() > 0) {
							try{
								value = Long.valueOf(et.getText().toString());
								if (value < 60 || value > 90) {
									sec1nextFocus = true;
									sec1viewId = R.id.heart_rate;
									et.getText().clear();
									openErrorDialog("Heart rate");

								}
							}catch(Exception e){
								Log.e("", e.toString());
							}
						}
						break;

					default:
						break;
					}
				} else {
					if (sec1nextFocus) {
						if (sec1viewId != 0) {
							try {
								View prev = findViewById(sec1viewId);
								prev.requestFocus();
								if (v.hasFocus()) {
									v.clearFocus();
								}
								((EditText) v).setCursorVisible(false);
								v.invalidate();
							} catch (Exception ex) {}
						}
					} else {
						((EditText) v).setCursorVisible(true);
						v.invalidate();
					}
				}
			}catch(Exception ex){
				Log.e("section1ETFocusChangeListner", ex.toString());
			}
			
		}
	};

	

	private void openErrorDialog(String title) {

		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle(title);
		alert.setMessage("Invalid data, enter again");
		alert.setCancelable(true);

		alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {

			}
		});

		alert.show();
	}

	public void checkSection1Data(int viewId) {

	}

	int sec1viewId = 0;
	boolean sec1nextFocus = false;

	int sec4viewId = 0;
	boolean sec4nextFocusPoint = false;

	int sec3viewId = 0;
	boolean sec3nextFocusPoint = false;
	
	int infoViewId = 0;
	boolean infoNextFocusPoint = false;
}
